package com.yeook.ymax.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yeook.ymax.R;
import com.yeook.ymax.ui.activitys.ResponsiveUIActivity;
import com.yeook.ymax.ui.entity.CateView;

public class ImageMenuFragment extends Fragment {
	
	public static CateView[] cates = new CateView[18];
	private ListView cateListView;
	private ResponsiveUIActivity activity = null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		activity = (ResponsiveUIActivity) getActivity();
		//cates = getResources().getStringArray(R.array.birds);
		cates[0] = new CateView(activity.getString(R.string.wallpaper_category_1), 1);
		cates[1] = new CateView(activity.getString(R.string.wallpaper_category_2), 2);
		cates[2] = new CateView(activity.getString(R.string.wallpaper_category_3), 3);
		cates[3] = new CateView(activity.getString(R.string.wallpaper_category_4), 4);
		cates[4] = new CateView(activity.getString(R.string.wallpaper_category_5), 5);
		cates[5] = new CateView(activity.getString(R.string.wallpaper_category_6), 6);
		cates[6] = new CateView(activity.getString(R.string.wallpaper_category_7), 7);
		cates[7] = new CateView(activity.getString(R.string.wallpaper_category_8), 8);
		cates[8] = new CateView(activity.getString(R.string.wallpaper_category_9), 9);
		cates[9] = new CateView(activity.getString(R.string.wallpaper_category_10), 10);
		cates[10] = new CateView(activity.getString(R.string.wallpaper_category_11), 11);
		cates[11] = new CateView(activity.getString(R.string.wallpaper_category_12), 12);
		cates[12] = new CateView(activity.getString(R.string.wallpaper_category_13), 13);
		cates[13] = new CateView(activity.getString(R.string.wallpaper_category_14), 14);
		cates[14] = new CateView(activity.getString(R.string.wallpaper_category_15), 15);
		cates[15] = new CateView(activity.getString(R.string.wallpaper_category_16), 16);
		cates[16] = new CateView(activity.getString(R.string.wallpaper_category_17), 17);
		cates[17] = new CateView(activity.getString(R.string.wallpaper_category_18), 18);
		
		View layout = inflater.inflate(R.layout.image_menu_frame, null);
		
		TextView title =  (TextView)layout.findViewById(R.id.home_title);
		title.setText(R.string.menu_wallaper);
		
		cateListView = (ListView) layout.findViewById(R.id.wallaper_cate_list);
		
		ItemAdapter adapter = new ItemAdapter();
		cateListView.setAdapter(adapter);
		
//		cateListView.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				Fragment newContent = new ImageGridFragment(cates[position].getCode(), cates[position].getName());
//				if (newContent != null)
//					switchFragment(newContent);
//			}
//			
//		});
		
		return layout;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		
	}
	
	class ItemAdapter extends BaseAdapter {

		private class ViewHolder {
			public TextView text;
			public ImageView image;
			public int code;
		}

		@Override
		public int getCount() {
			return cates.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View view = convertView;
			final ViewHolder holder;
			if (convertView == null) {
				view = activity.getLayoutInflater().inflate(R.layout.image_cate_item_list_image, parent, false);
				holder = new ViewHolder();
				holder.text = (TextView) view.findViewById(R.id.text);
				holder.image = (ImageView) view.findViewById(R.id.image);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}

			holder.text.setText(cates[position].getName());
			holder.code = cates[position].getCode();

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					Fragment newContent = new ImageGridFragment(cates[position].getCode(), cates[position].getName());
					if (newContent != null)
						switchFragment(newContent);
				}
				
			});

			return view;
		}
	}
	
	// the meat of switching the above fragment
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof ResponsiveUIActivity) {
			ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
			ra.switchContent(fragment);
		}
	}
}
